﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Xml;

namespace jihuang_Player
{
    class Debin
    {
        public byte[] build;
        public int mark = 0;
        public bool IsLitteEndian;
        private TEXTool.TEXTool tool;
        public int AltersbitmapWidth = 0;
        public int AltersbitmapHeight = 0;
        public SysmbolsName[] SysmbolsNames;
        public string targetPath;
        public string otherAnPath;

        public void publicDebin()
        {
            if (tool == null)
            {
                tool = new TEXTool.TEXTool();
            }
            IsLitteEndian = BitConverter.IsLittleEndian;
        }
        public Debin(string path)
        {
            publicDebin();
            otherAnPath = path;
        }
        public Debin(MainWindow w, string path, bool deff)
        {
            publicDebin();
            targetPath = path;
            ArrayList symbolList; ;

            build = File.ReadAllBytes(path + @"\build.bin");
            w.showinfo("类型：" + readHead());
            w.showinfo("版本：" + readInt());
            int symbols = readInt();
            symbolList = new ArrayList();
            w.showinfo("symbols;" + symbols);
            Console.WriteLine("frames:" + readInt());
            string BuildName = readBuildName();
            w.showinfo("BuildName:" + BuildName);
            int alters = readInt();
            Console.WriteLine("alters:" + alters);
            for (int i = 0; i < alters; i++)
            {
                string texname = readAlter();
                w.showinfo("打开：" + texname);
                FileStream stream = new FileStream(path + @"\" + texname, FileMode.Open);
                tool.OpenFile("woc.cnm", stream);
                stream.Close();
                AltersbitmapWidth = tool.CurrentFileRaw.Width;
                AltersbitmapHeight = tool.CurrentFileRaw.Height;
                Console.WriteLine("alter:" + texname);
            }
            for (int k = 0; k < symbols; k++)
            {
                Symbol symbolItem = new Symbol();
                int str_hash = readInt();
                Console.WriteLine("str_hash:" + str_hash);
                int Frame_number = readInt();
                Console.WriteLine("Frame number:" + Frame_number);
                for (int i = 0; i < Frame_number; i++)
                {
                    Bbox box = new Bbox();
                    Console.WriteLine(" framenum:" + readInt());
                    Console.WriteLine(" duration:" + readInt());
                    box.idx = str_hash;
                    float x = readFloat();
                    box.x = x;
                    Console.WriteLine("     x:" + x);
                    float y = readFloat();
                    box.y = y;
                    Console.WriteLine("     y:" + y);
                    float ww = readFloat();
                    box.w = ww;
                    Console.WriteLine("     w:" + ww);
                    float h = readFloat();
                    box.h = h;
                    Console.WriteLine("     h:" + h);
                    int start = readInt();
                    box.start = start;
                    Console.WriteLine("     alphaidx:" + start);
                    int count = readInt();
                    box.count = count;
                    Console.WriteLine("     alphacount:" + count);
                    symbolItem.boxlist.Add(box);
                }
                symbolList.Add(symbolItem);
            }
            int alphaverts = readInt();
            Console.WriteLine("\n\n\n\n--------------------------------------alphaverts:" + alphaverts);
            Point[] point = new Point[alphaverts];
            for (int z = 0; z < alphaverts; z++)
            {
                Point p = new Point();
                float x = readFloat();
                float y = readFloat();
                float zz = readFloat();
                float u = readFloat();
                float v = readFloat();
                float ww = readFloat();
                p.x = x;
                p.y = y;
                p.u = u;
                p.v = v;
                point[z] = p;
                //Console.WriteLine("x:" + x + "  y:" + y + "  z:" + zz + "  u:" + u + "  v:" + v + "  w:" + ww);
            }

            int hashcollection = readInt();
            Console.WriteLine("\n\n\n\n--------------------------------------hashcollection:" + hashcollection);
            SysmbolsNames = new SysmbolsName[hashcollection];
            for (int y = 0; y < hashcollection; y++)
            {
                SysmbolsName sysmbolname = new SysmbolsName();
                int hash_idx = readInt();
                int len = readInt();
                string sname = readString(len);
                sysmbolname.idx = hash_idx;
                sysmbolname.name = sname;
                SysmbolsNames[y] = sysmbolname;
                Console.WriteLine("hash_idx:" + hash_idx + "   " + sname);
            }

            foreach (Symbol symbol in symbolList)
            {
                int suoyin = 0;
                foreach (Bbox box in symbol.boxlist)
                {
                    string dir = path + @"\反编译\";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string sstr = getSysbolNmame(box.idx);
                    symbol.SymbolName = sstr;
                    string sssname = sstr + "-" + suoyin + ".png";
                    FileStream wimage = new FileStream(path + @"\反编译\" + sssname, FileMode.Create);
                    w.showinfo("切割：" + sssname);

                    if (deff)
                    {
                        Point temppoint = point[box.start];
                        int imageWidth = (int)box.w;
                        int imageHeight = (int)box.h;
                        int ventx = (int)(AltersbitmapWidth * temppoint.u);
                        int venty = (int)(AltersbitmapHeight * temppoint.v);
                        int imageCentenX = imageWidth / 2;
                        int imageCentenY = imageHeight / 2;
                        int cutX = (int)(ventx - temppoint.x + box.x - imageCentenX);
                        int cutY = (int)(AltersbitmapHeight - (venty + temppoint.y - box.y - imageCentenY + box.h));

                        Rectangle cloneRect = new Rectangle(cutX, cutY, imageWidth, imageHeight);
                        try
                        {
                            Bitmap cutBitmap = tool.CurrentFileRaw.Clone(cloneRect, tool.CurrentFileRaw.PixelFormat);
                            cutBitmap.Save(wimage, ImageFormat.Png);
                        }
                        catch (Exception e)
                        {

                            Console.WriteLine("失败" + e.ToString());
                            w.showinfo("反编译失败！！！请使用通用反编译");
                            wimage.Close();
                            return;
                        }
                    }
                    else
                    {
                        Point temppoint = new Point();
                        float mini_u = 1;
                        float mini_v = 1;
                        float max_u = 0;
                        float max_v = 0;
                        float x = 0;
                        float y = 0;
                        for (int ii = 0; ii < box.count; ii++)
                        {
                            Point thisPoint = point[box.start + ii];
                            if (thisPoint.u < mini_u)
                            {
                                mini_u = thisPoint.u;
                                x = thisPoint.x;
                            }
                            if (thisPoint.v < mini_v)
                            {
                                mini_v = thisPoint.v;
                                y = thisPoint.y;
                            }

                            if (thisPoint.u > max_u)
                            {
                                max_u = thisPoint.u;
                            }
                            if (thisPoint.v > max_v)
                            {
                                max_v = thisPoint.v;
                            }
                        }
                        temppoint.u = mini_u;
                        temppoint.v = mini_v;
                        temppoint.x = x;
                        temppoint.y = y;

                        int imagewidth = (int)(AltersbitmapWidth * (max_u - mini_u));
                        int imageheight = (int)(AltersbitmapHeight * (max_v - mini_v));

                        Rectangle cloneRect = new Rectangle((int)(mini_u * AltersbitmapWidth), (int)(AltersbitmapHeight - max_v * AltersbitmapHeight), imagewidth, imageheight);
                        System.Drawing.Imaging.PixelFormat format = tool.CurrentFileRaw.PixelFormat;
                        Bitmap cloneBitmap = tool.CurrentFileRaw.Clone(cloneRect, format);
                        Bitmap resultBitmap = new Bitmap(cloneBitmap, (int)box.w, (int)box.h);
                        resultBitmap.Save(wimage, ImageFormat.Png);
                        cloneBitmap = null;
                        resultBitmap = null;
                    }
                    wimage.Close();
                    suoyin++;
                }
            }
            w.showinfo("构建：build");
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement Build = xmlDocument.CreateElement("Build");
            XmlAttribute name = xmlDocument.CreateAttribute("name");
            name.Value = BuildName + ".scml";
            Build.Attributes.Append(name);

            foreach (Symbol symbol in symbolList)
            {
                XmlElement Symbol = xmlDocument.CreateElement("Symbol");
                XmlAttribute sysname = xmlDocument.CreateAttribute("name");
                sysname.Value = symbol.SymbolName;
                Symbol.Attributes.Append(sysname);
                int count = 0;
                foreach (Bbox box in symbol.boxlist)
                {
                    XmlElement Frame = xmlDocument.CreateElement("Frame");
                    XmlAttribute framenum = xmlDocument.CreateAttribute("framenum");
                    framenum.Value = "" + count;
                    Frame.Attributes.Append(framenum);
                    XmlAttribute duration = xmlDocument.CreateAttribute("duration");
                    duration.Value = "1";
                    Frame.Attributes.Append(duration);

                    XmlAttribute image = xmlDocument.CreateAttribute("image");
                    image.Value = symbol.SymbolName + "-" + count;
                    Frame.Attributes.Append(image);

                    XmlAttribute www = xmlDocument.CreateAttribute("w");
                    www.Value = "" + box.w;
                    Frame.Attributes.Append(www);

                    XmlAttribute hhh = xmlDocument.CreateAttribute("h");
                    hhh.Value = "" + box.h;
                    Frame.Attributes.Append(hhh);

                    XmlAttribute xxx = xmlDocument.CreateAttribute("x");
                    xxx.Value = "" + box.x;
                    Frame.Attributes.Append(xxx);

                    XmlAttribute yyy = xmlDocument.CreateAttribute("y");
                    yyy.Value = "" + box.y;
                    Frame.Attributes.Append(yyy);

                    Symbol.AppendChild(Frame);
                    count++;
                }
                Build.AppendChild(Symbol);
            }
            xmlDocument.AppendChild(Build);
            xmlDocument.Save(path + @"\反编译\build.xml");

            decodean(w);
            w.showinfo("反编译完成！");
        }

        public string getAn()
        {
            Hashtable symbols = new Hashtable();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement Anims = xmldoc.CreateElement("Anims");
            mark = 0;
            readBytes(4, true);
            int version = readInt();
            int element_number = readInt();
            int frame_number = readInt();
            int event_number = readInt();
            int anim_number = readInt();
            for (int i = 0; i < anim_number; i++)
            {
                Console.WriteLine("-----------------------------------------");
                int name_len = readInt();
                string name = readString(name_len);
                Console.WriteLine("name:" + name);

                byte facingbyte = readBytes(1)[0];
                Console.WriteLine("?facingbyte:" + facingbyte);
                int strhash = readInt();
                Console.WriteLine("strhash:" + strhash);
                float frame_rate = readFloat();
                Console.WriteLine("frame_rate:" + frame_rate);
                int num_frames = readInt();
                Console.WriteLine("num_frames:" + num_frames);

                XmlElement anim = xmldoc.CreateElement("anim");
                XmlAttribute name2 = xmldoc.CreateAttribute("name");
                XmlAttribute root = xmldoc.CreateAttribute("root");
                XmlAttribute numframes = xmldoc.CreateAttribute("numframes");
                XmlAttribute framerate = xmldoc.CreateAttribute("framerate");

                name2.Value = name;
                root.Value = "" + strhash;
                numframes.Value = "" + num_frames;
                framerate.Value = "" + frame_rate;

                anim.Attributes.Append(name2);
                anim.Attributes.Append(root);
                anim.Attributes.Append(numframes);
                anim.Attributes.Append(framerate);
                for (int j = 0; j < num_frames; j++)
                {
                    //Console.WriteLine("    -------------------------------------");
                    float x = readFloat();
                    float y = readFloat();
                    float w = readFloat();
                    float h = readFloat();
                    //Console.WriteLine("    x:" + x + "  y:" + y + "  w:" + w + "  h:" + h);
                    int num_events = readInt();
                    // Console.WriteLine("    num_events:" + num_events);
                    for (int k = 0; k < num_events; k++)
                    {
                        int strhash2 = readInt();
                        Console.WriteLine("        strhash:" + strhash2);
                    }

                    int num_elements = readInt();
                    //Console.WriteLine("    num_elements:" + num_elements);

                    XmlElement frame = xmldoc.CreateElement("frame");
                    XmlAttribute idx = xmldoc.CreateAttribute("idx");
                    XmlAttribute ww = xmldoc.CreateAttribute("w");
                    XmlAttribute hh = xmldoc.CreateAttribute("h");
                    XmlAttribute xx = xmldoc.CreateAttribute("x");
                    XmlAttribute yy = xmldoc.CreateAttribute("y");

                    idx.Value = "idx";
                    ww.Value = "" + w;
                    hh.Value = "" + h;
                    xx.Value = "" + x;
                    yy.Value = "" + y;

                    frame.Attributes.Append(idx);
                    frame.Attributes.Append(ww);
                    frame.Attributes.Append(hh);
                    frame.Attributes.Append(xx);
                    frame.Attributes.Append(yy);
                    for (int k = 0; k < num_elements; k++)
                    {
                        XmlElement element = xmldoc.CreateElement("element");

                        //Console.WriteLine("        ---------------------------------");
                        int symbol_hash = readInt();
                        // Console.WriteLine("        symbol_hash:" + symbol_hash);
                        int symbol_frame = readInt();
                        // Console.WriteLine("        symbol_frame:" + symbol_frame);
                        int folder_hash = readInt();
                        // Console.WriteLine("        folder hash:" + folder_hash);
                        float ma = readFloat();
                        float mb = readFloat();
                        float mc = readFloat();
                        float md = readFloat();
                        float mtx = readFloat();
                        float mty = readFloat();
                        float mtz = readFloat();
                        // Console.WriteLine("        ma:" + ma + "  mb:" + mb + "  mc:" + mc + "  md:" + md + "  m_tx:" + mtx + "  m_ty:" + mty + "  m_tz:" + mtz);
                        // Console.WriteLine("        ---------------------------------");

                        XmlAttribute frame2 = xmldoc.CreateAttribute("frame");
                        XmlAttribute layername = xmldoc.CreateAttribute("layername");
                        XmlAttribute name3 = xmldoc.CreateAttribute("name");
                        XmlAttribute z_index = xmldoc.CreateAttribute("z_index");
                        XmlAttribute m_a = xmldoc.CreateAttribute("m_a");
                        XmlAttribute m_b = xmldoc.CreateAttribute("m_b");
                        XmlAttribute m_c = xmldoc.CreateAttribute("m_c");
                        XmlAttribute m_d = xmldoc.CreateAttribute("m_d");
                        XmlAttribute m_tx = xmldoc.CreateAttribute("m_tx");
                        XmlAttribute m_ty = xmldoc.CreateAttribute("m_ty");

                        frame2.Value = "" + symbol_frame;
                        layername.Value = "" + folder_hash;
                        name3.Value = "" + symbol_hash;
                        z_index.Value = "" + (int)(mtz * 10);
                        m_a.Value = "" + ma;
                        m_b.Value = "" + mb;
                        m_c.Value = "" + mc;
                        m_d.Value = "" + md;
                        m_tx.Value = "" + mtx;
                        m_ty.Value = "" + mty;

                        element.Attributes.Append(frame2);
                        element.Attributes.Append(layername);
                        element.Attributes.Append(name3);
                        element.Attributes.Append(z_index);
                        element.Attributes.Append(m_a);
                        element.Attributes.Append(m_b);
                        element.Attributes.Append(m_c);
                        element.Attributes.Append(m_d);
                        element.Attributes.Append(m_tx);
                        element.Attributes.Append(m_ty);
                        frame.AppendChild(element);
                    }
                    // Console.WriteLine("    -------------------------------------");
                    anim.AppendChild(frame);
                }
                Console.WriteLine("-----------------------------------------");

                Anims.AppendChild(anim);
            }
            int num_hashed = readInt();
            for (int l = 0; l < num_hashed; l++)
            {
                int hash = readInt();
                int namelen = readInt();
                string name = readString(namelen);
                symbols.Add("" + hash, name);
            }

            XmlNodeList anims = xmldoc.GetElementsByTagName("anim");
            xmldoc.AppendChild(Anims);
            foreach (XmlNode node in anims)
            {
                XmlAttribute root = node.Attributes["root"];
                root.Value = (string)symbols[root.Value];
                XmlNodeList frames = node.SelectNodes("frame");
                foreach (XmlNode node2 in frames)
                {
                    XmlNodeList elements = node2.SelectNodes("element");
                    foreach (XmlNode node3 in elements)
                    {
                        XmlAttribute layername = node3.Attributes["layername"];
                        layername.Value = (string)symbols[layername.Value];
                        XmlAttribute name = node3.Attributes["name"];
                        name.Value = (string)symbols[name.Value];
                    }
                }
            }
            return ConvertXmlToString(xmldoc);
        }
        public string decodeanzip()
        {
            ZipArchive zip = ZipFile.OpenRead(otherAnPath);
            ZipArchiveEntry animfile = zip.GetEntry("anim.bin");
            Stream read = animfile.Open();

            ArrayList list = new ArrayList();

            int offset = 0;
            while (true)
            {
                byte[] temp = new byte[1024];
                if (read.Read(temp, 0, 1024) != 0)
                {
                    offset += 1024;
                    list.Add(temp);
                }
                else
                {
                    break;
                }
            }

            int len = list.Count * 1024;
            build = new byte[len];

            int mark = 0;
            foreach (byte[] b in list)
            {
                foreach (byte bb in b)
                {
                    build[mark] = bb;
                    mark++;
                }
            }
            read.Close();
            zip.Dispose();
            return getAn();
        }
        public string decodean()
        {
            build = File.ReadAllBytes(otherAnPath);
            return getAn();
        }
        public string ConvertXmlToString(XmlDocument xmlDoc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = Formatting.Indented; xmlDoc.Save(writer);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();
            return xmlString;
        }
        public void decodean(MainWindow mw)
        {
            Hashtable symbols = new Hashtable();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement Anims = xmldoc.CreateElement("Anims");

            mw.showinfo("打开：anim.bin");
            build = File.ReadAllBytes(targetPath + @"\anim.bin");
            mark = 0;
            readBytes(4, true);
            int version = readInt();
            mw.showinfo("version:" + version);
            int element_number = readInt();
            mw.showinfo("element_number:" + element_number);
            int frame_number = readInt();
            mw.showinfo("frame_number:" + frame_number);
            int event_number = readInt();
            mw.showinfo("event_number:" + event_number);
            int anim_number = readInt();
            mw.showinfo("anim_number:" + anim_number);
            for (int i = 0; i < anim_number; i++)
            {
                Console.WriteLine("-----------------------------------------");
                int name_len = readInt();
                string name = readString(name_len);
                Console.WriteLine("name:" + name);
                mw.showinfo("反编译动画：" + name);
                byte facingbyte = readBytes(1)[0];
                Console.WriteLine("?facingbyte:" + facingbyte);
                int strhash = readInt();
                Console.WriteLine("strhash:" + strhash);
                float frame_rate = readFloat();
                Console.WriteLine("frame_rate:" + frame_rate);
                int num_frames = readInt();
                Console.WriteLine("num_frames:" + num_frames);

                XmlElement anim = xmldoc.CreateElement("anim");
                XmlAttribute name2 = xmldoc.CreateAttribute("name");
                XmlAttribute root = xmldoc.CreateAttribute("root");
                XmlAttribute numframes = xmldoc.CreateAttribute("numframes");
                XmlAttribute framerate = xmldoc.CreateAttribute("framerate");

                name2.Value = name;
                root.Value = "" + strhash;
                numframes.Value = "" + num_frames;
                framerate.Value = "" + frame_rate;

                anim.Attributes.Append(name2);
                anim.Attributes.Append(root);
                anim.Attributes.Append(numframes);
                anim.Attributes.Append(framerate);
                for (int j = 0; j < num_frames; j++)
                {
                    //Console.WriteLine("    -------------------------------------");
                    float x = readFloat();
                    float y = readFloat();
                    float w = readFloat();
                    float h = readFloat();
                    //Console.WriteLine("    x:" + x + "  y:" + y + "  w:" + w + "  h:" + h);
                    int num_events = readInt();
                    // Console.WriteLine("    num_events:" + num_events);
                    for (int k = 0; k < num_events; k++)
                    {
                        int strhash2 = readInt();
                        Console.WriteLine("        strhash:" + strhash2);
                    }

                    int num_elements = readInt();
                    //Console.WriteLine("    num_elements:" + num_elements);

                    XmlElement frame = xmldoc.CreateElement("frame");
                    XmlAttribute idx = xmldoc.CreateAttribute("idx");
                    XmlAttribute ww = xmldoc.CreateAttribute("w");
                    XmlAttribute hh = xmldoc.CreateAttribute("h");
                    XmlAttribute xx = xmldoc.CreateAttribute("x");
                    XmlAttribute yy = xmldoc.CreateAttribute("y");

                    idx.Value = "idx";
                    ww.Value = "" + w;
                    hh.Value = "" + h;
                    xx.Value = "" + x;
                    yy.Value = "" + y;

                    frame.Attributes.Append(idx);
                    frame.Attributes.Append(ww);
                    frame.Attributes.Append(hh);
                    frame.Attributes.Append(xx);
                    frame.Attributes.Append(yy);
                    for (int k = 0; k < num_elements; k++)
                    {
                        XmlElement element = xmldoc.CreateElement("element");

                        //Console.WriteLine("        ---------------------------------");
                        int symbol_hash = readInt();
                        // Console.WriteLine("        symbol_hash:" + symbol_hash);
                        int symbol_frame = readInt();
                        // Console.WriteLine("        symbol_frame:" + symbol_frame);
                        int folder_hash = readInt();
                        // Console.WriteLine("        folder hash:" + folder_hash);
                        float ma = readFloat();
                        float mb = readFloat();
                        float mc = readFloat();
                        float md = readFloat();
                        float mtx = readFloat();
                        float mty = readFloat();
                        float mtz = readFloat();
                        // Console.WriteLine("        ma:" + ma + "  mb:" + mb + "  mc:" + mc + "  md:" + md + "  m_tx:" + mtx + "  m_ty:" + mty + "  m_tz:" + mtz);
                        // Console.WriteLine("        ---------------------------------");

                        XmlAttribute frame2 = xmldoc.CreateAttribute("frame");
                        XmlAttribute layername = xmldoc.CreateAttribute("layername");
                        XmlAttribute name3 = xmldoc.CreateAttribute("name");
                        XmlAttribute z_index = xmldoc.CreateAttribute("z_index");
                        XmlAttribute m_a = xmldoc.CreateAttribute("m_a");
                        XmlAttribute m_b = xmldoc.CreateAttribute("m_b");
                        XmlAttribute m_c = xmldoc.CreateAttribute("m_c");
                        XmlAttribute m_d = xmldoc.CreateAttribute("m_d");
                        XmlAttribute m_tx = xmldoc.CreateAttribute("m_tx");
                        XmlAttribute m_ty = xmldoc.CreateAttribute("m_ty");

                        frame2.Value = "" + symbol_frame;
                        layername.Value = "" + folder_hash;
                        name3.Value = "" + symbol_hash;
                        z_index.Value = "" + (int)(mtz * 10);
                        m_a.Value = "" + ma;
                        m_b.Value = "" + mb;
                        m_c.Value = "" + mc;
                        m_d.Value = "" + md;
                        m_tx.Value = "" + mtx;
                        m_ty.Value = "" + mty;

                        element.Attributes.Append(frame2);
                        element.Attributes.Append(layername);
                        element.Attributes.Append(name3);
                        element.Attributes.Append(z_index);
                        element.Attributes.Append(m_a);
                        element.Attributes.Append(m_b);
                        element.Attributes.Append(m_c);
                        element.Attributes.Append(m_d);
                        element.Attributes.Append(m_tx);
                        element.Attributes.Append(m_ty);
                        frame.AppendChild(element);
                    }
                    // Console.WriteLine("    -------------------------------------");
                    anim.AppendChild(frame);
                }
                Console.WriteLine("-----------------------------------------");

                Anims.AppendChild(anim);
            }
            int num_hashed = readInt();
            for (int l = 0; l < num_hashed; l++)
            {
                int hash = readInt();
                int namelen = readInt();
                string name = readString(namelen);
                symbols.Add("" + hash, name);
            }
            mw.showinfo("刷新hash");
            XmlNodeList anims = xmldoc.GetElementsByTagName("anim");
            xmldoc.AppendChild(Anims);
            foreach (XmlNode node in anims)
            {
                XmlAttribute root = node.Attributes["root"];
                root.Value = (string)symbols[root.Value];
                XmlNodeList frames = node.SelectNodes("frame");
                foreach (XmlNode node2 in frames)
                {
                    XmlNodeList elements = node2.SelectNodes("element");
                    foreach (XmlNode node3 in elements)
                    {
                        XmlAttribute layername = node3.Attributes["layername"];
                        layername.Value = (string)symbols[layername.Value];
                        XmlAttribute name = node3.Attributes["name"];
                        name.Value = (string)symbols[name.Value];
                    }
                }
            }
            mw.showinfo("动画反编译完成！正在写入文件...");
            xmldoc.Save(targetPath + @"\反编译\animation.xml");
        }

        public string readHead()
        {
            byte[] head = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                head[i] = build[mark];
                mark++;
            }
            return System.Text.Encoding.ASCII.GetString(head);
        }
        public byte[] readBytes(int count)
        {
            byte[] b = new byte[count];
            for (int i = 0; i < count; i++)
            {
                b[i] = build[mark];
                mark++;
            }
            return b;
        }

        public byte[] readBytes(int count, bool show)
        {
            byte[] b = new byte[count];
            for (int i = 0; i < count; i++)
            {
                b[i] = build[mark];
                mark++;
                Console.WriteLine(b[i]);
            }
            return b;
        }

        public int readInt()
        {
            byte[] b = readBytes(4);
            if (IsLitteEndian)
            {
                return BitConverter.ToInt32(b, 0);
            }
            else
            {
                byte[] temp = new byte[4];
                temp[0] = b[3];
                temp[1] = b[2];
                temp[2] = b[1];
                temp[3] = b[0];
                return BitConverter.ToInt32(temp, 0);
            }

        }

        public float readFloat()
        {
            byte[] b = readBytes(4);
            if (IsLitteEndian)
            {
                return BitConverter.ToSingle(b, 0);
            }
            else
            {
                byte[] temp = new byte[4];
                temp[0] = b[3];
                temp[1] = b[2];
                temp[2] = b[1];
                temp[3] = b[0];
                return BitConverter.ToSingle(temp, 0);
            }
        }
        public string readString(int len)
        {
            byte[] str = new byte[len];
            for (int i = 0; i < len; i++)
            {
                str[i] = build[mark];
                mark++;
            }
            return System.Text.Encoding.ASCII.GetString(str);
        }
        public string readBuildName()
        {
            int len = readInt();
            return readString(len);
        }

        public string readAlter()
        {
            int len = readInt();
            return readString(len);
        }

        public string getSysbolNmame(int idx)
        {
            string name1 = null;
            foreach (SysmbolsName name in SysmbolsNames)
            {
                if (name.idx == idx)
                {
                    name1 = name.name;
                    break;
                }
            }
            return name1;
        }
    }
    class Bbox
    {
        public float x;
        public float y;
        public float w;
        public float h;
        public int start;
        public int count;
        public int idx;
    }
    class Symbol
    {
        public ArrayList boxlist = new ArrayList();
        public string SymbolName;
    }
    class Point
    {
        public float x;
        public float y;
        public float u;
        public float v;
    }
    class SysmbolsName
    {
        public int idx;
        public string name;
    }
}
